package com.wangjiacai.hqlQuery.role.service.impl;

import com.wangjiacai.hqlQuery.repository.BaseRepository;
import com.wangjiacai.hqlQuery.role.converter.RoleConverter;
import com.wangjiacai.hqlQuery.role.entity.Role;
import com.wangjiacai.hqlQuery.role.entity.RoleFunction;
import com.wangjiacai.hqlQuery.role.entity.UserRole;
import com.wangjiacai.hqlQuery.role.repository.FunctionRepository;
import com.wangjiacai.hqlQuery.role.repository.RoleFunctionRepository;
import com.wangjiacai.hqlQuery.role.repository.RoleRepository;
import com.wangjiacai.hqlQuery.role.repository.UserRoleRepository;
import com.wangjiacai.hqlQuery.role.service.RoleService;
import com.wangjiacai.hqlQuery.service.impl.BaseServiceImpl;
import com.wangjiacai.hqlQuery.util.HqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/*
 * @auther: wangjiacai
 *
 * @create: 2019-01-27 18:09:41
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl implements RoleService {

    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private FunctionRepository functionRepository;
    @Autowired
    private RoleFunctionRepository roleFunctionRepository;
    @Autowired
    private UserRoleRepository userRoleRepository;

    @Override
    public long getRoleListNum(Role role) {
        return this.count(HqlUtil.getCountHql(role), role);
    }

    @Override
    public List<Role> getRoleList(Role role, Pageable pageable) {
        return this.find(HqlUtil.getQueryHql(role), role, pageable, Role.class);
    }

    @Override
    public List<Role> getRoleList(Role role) {
        return this.find(HqlUtil.getQueryHql(role), role,Role.class);
    }

    @Override
    public void assignRoleFunctionByBhs(String functionIds, String roleId) {
        roleFunctionRepository.deleteByRoleId(roleId);
        for (String bh : functionIds.split(",")) {
            String functionId = functionRepository.findFunctionIdByBh(bh);
            RoleFunction roleFunction = new RoleFunction();
            roleFunction.setFunctionId(functionId);
            roleFunction.setRoleId(roleId);
            roleFunctionRepository.save(roleFunction);
        }
    }

    @Override
    public void assignUserRoleByRoleIds(String roleIds, String userId) {
        userRoleRepository.deleteByUserId(userId);
        String[] roleIdArray=roleIds.split(",");
        for (String roleId : roleIdArray) {
            if(roleId.trim().equals("")) continue;
            UserRole userRole = new UserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            userRoleRepository.save(userRole);
        }
    }

    @Override
    public Role addRole(Role role) {
        return roleRepository.save(role);
    }

    @Override
    public void deleteRole(String roleId) {
        roleFunctionRepository.deleteByRoleId(roleId);
        roleRepository.deleteById(roleId);
    }

    @Override
    public List<Role> getAllRoleList() {
        return roleRepository.findAll();
    }

    @Override
    public List<String> findRoleIdListByUserId(String userId) {
        return userRoleRepository.findRoleIdByUserId(userId);
    }

    @Override
    public List<Map> getRoleTreeMap(Role role) {
        List<Role> list;
        if (role == null) list = roleRepository.findAll();
        else list = this.find(HqlUtil.getQueryHql(role), role, Role.class);
        return RoleConverter.converterToMapList(list);
    }


}
